﻿using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using Maticsoft.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FangChanCrm.Controllers
{
    public class GangWeiController : Controller
    {
        private readonly It_GangWeiBiaoService t_GangWeiBiaoService;

        public GangWeiController(It_GangWeiBiaoService gangWeiBiaoService, IConfiguration configuration)
        {
            t_GangWeiBiaoService = gangWeiBiaoService;
            Configuration = configuration;
        }

        public IActionResult GangWeiAdd()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
        public IActionResult GangWeiList()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
        public IActionResult GangWeiXiuGai()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        public string BangDingBuMen()
        {
            List<t_BuMenBiao> list = t_GangWeiBiaoService.BangDingBuMen();
            string json = "[";
            for (int i = 0; i < list.Count; i++)
            {
                    json += "{";
                    json += "\"BuMenMingCheng\":\"" + list[i].BuMenMingCheng + "\",\"Id\":\"" + list[i].Id + "\"";
                    json += "}";
                    if (i != list.Count - 1)
                    {
                        json += ",";
                    }
                

            }
            json += "]";
            return json;
        }
        public string TianJiaGangWei()
        {
            string ipt_GangWeiMingCheng = Request.Form["ipt_GangWeiMingCheng"];
            int slt_SuoSuBuMen = Convert.ToInt32(Request.Form["slt_SuoSuBuMen"]);
            string ipt_GangWeilLeiXing = Request.Form["ipt_GangWeilLeiXing"];
            string ipt_GangWeiDengJi = Request.Form["ipt_GangWeiDengJi"];
            t_GangWeiBiao t_GangWeiBiao = new t_GangWeiBiao();
            t_GangWeiBiao.GangWeiMingCheng = ipt_GangWeiMingCheng;
            t_GangWeiBiao.BuMenId = slt_SuoSuBuMen;
            t_GangWeiBiao.GangWeiLeiXing = ipt_GangWeilLeiXing;
            t_GangWeiBiao.DengJi = Convert.ToInt32(ipt_GangWeiDengJi);
            string shuju = t_GangWeiBiaoService.GangWeiAdd(t_GangWeiBiao);
            return shuju;
        }
        public string GangWeiLB()
        {
            string ipt_GangWeiMingCheng = Request.Form["ipt_GangWeiMingCheng"];
            string slt_SuoShuBuMen = Request.Form["slt_SuoShuBuMen"];
            string ipt_GangWeiLeiXing = Request.Form["ipt_GangWeiLeiXing"];
            string ipt_GangWeiDengJi = Request.Form["ipt_GangWeiDengJi"];
            t_GangWeiBiao GangWei = new t_GangWeiBiao();
            GangWei.BeiZhu1 = "";

            if (ipt_GangWeiMingCheng != "")
            {
                GangWei.GangWeiMingCheng = ipt_GangWeiMingCheng;
            }
            if (slt_SuoShuBuMen != "")
            {
                if (slt_SuoShuBuMen != "0")
                {
                    GangWei.BuMenId = Convert.ToInt32(slt_SuoShuBuMen);
                }
            }
            if (ipt_GangWeiLeiXing != "")
            {
                GangWei.GangWeiLeiXing = ipt_GangWeiLeiXing;
            }
            if (ipt_GangWeiDengJi != "")
            {
                GangWei.DengJi = Convert.ToInt32(ipt_GangWeiDengJi);
            }

            string shuju = t_GangWeiBiaoService.GetListWithFenYe(GangWei, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));

            if (t_GangWeiBiaoService.listshuju().ToList().Count != 0)
            {
                return shuju;
            }
            else
            {
                string jsonString = "{\"draw\":1,\"total\":0,\"total\":0,";
                jsonString += "\"" + "data" + "\":" + "[]" + ",";
                jsonString = jsonString.TrimEnd(',');
                return jsonString + "}";
                //return "[]";
            }
        }
        public string HuiBangShuJu()
        {
            int id = Convert.ToInt32(Request.Form["id"]);
            List<t_GangWeiBiao> list = t_GangWeiBiaoService.huibangQiYelist(id);
            string json = "";
            json += "[";
            for (int i = 0; i < list.Count; i++)
            {
                json += "{";
                json += "\"GangWeiMingCheng\":\"" + list[i].GangWeiMingCheng + "\",\"GangWeiLeiXing\":\"" + list[i].GangWeiLeiXing + "\",\"BuMenId\":\"" + list[i].BuMenId + "\",\"DengJi\":\"" + list[i].DengJi + "\"";
                json += "}";
                if (i != list.Count - 1)
                {
                    json += ",";
                }
            }
            json += "]";
            return json;
        }

        public IConfiguration Configuration { get; }

        [HttpPost]
        public string XiuGaiGangWei() 
        {
            int GangWeiId = Convert.ToInt32(Request.Form["Id"]);
            string ipt_GangWeiMingCheng = Request.Form["ipt_GangWeiMingCheng"];
            string slt_SuoSuBuMen = Request.Form["slt_SuoSuBuMen"];
            string ipt_GangWeilLeiXing = Request.Form["ipt_GangWeilLeiXing"];
            string ipt_GangWeiDengJi = Request.Form["ipt_GangWeiDengJi"];

            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var GangWei = _context.t_GangWeiBiao.FirstOrDefault(x => x.Id == GangWeiId);
                GangWei.GangWeiMingCheng = ipt_GangWeiMingCheng;
                GangWei.BuMenId = Convert.ToInt32(slt_SuoSuBuMen);
                GangWei.GangWeiLeiXing = ipt_GangWeilLeiXing;
                GangWei.DengJi = Convert.ToInt32(ipt_GangWeiDengJi);
                panduan = _context.SaveChanges() > 0;
            }

            if (panduan)
            {
                return "success";
            }
            else 
            {
                return "error";
            }
        } 
    }
}